<template>
  <div style="padding-top: 46px">
    <template v-if="user" >
      <van-cell title="当前用户" :value="user?.nickName"/>
      <van-cell title="修改信息" is-link to="/my/update"/>
      <van-cell title="我加入的队伍" is-link to="/my/teams"/>
    </template>
  </div>
  <div style="display: flex; justify-content: center; align-items: center; height: 100vh;">
    <van-button type="default" @click="logout">退出登录</van-button>
  </div>

</template>

<script setup lang="ts">
import {useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import {getCurrentUser} from "../services/user.ts";
import myAxios from "../plugins/myAxios.ts";

// const user = {
//   id: 1,
//   username: '鱼皮',
//   userAccount: 'dogYupi',
//   avatarUrl: 'https://636f-codenav-8grj8px727565176-1256524210.tcb.qcloud.la/img/logo.png',
//   gender: '男',
//   phone: '123112312',
//   email: '12345@qq.com',
//   planetCode: '1234',
//   createTime: new Date(),
// }

const user = ref();

const router = useRouter();
onMounted(async () => {
  const res = await getCurrentUser();
  // @ts-ignore
  if (res?.code === 40100||res.data==null) {
    router.push({
      path: '/my/login',
      replace: true,
    });
  }
  // @ts-ignore
  if (res.code === 200) {
    console.log("登录成功")
    user.value = res.data
  }

})

// const toEdit = (editKey: string, editName: string, currentValue: string) => {
//   router.push({
//     path: '/my/edit',
//     query: {
//       editKey,
//       editName,
//       currentValue,
//     }
//   })
// }

const logout = async () => {
  try {
    const res = await myAxios.post('/logout');
    // @ts-ignore
    if (res?.code === 200) {
      console.log("退出登录");
      await router.replace("/team");
    } else {
      console.log("失败");
    }
  } catch (error) {
    console.error("请求失败", error);
  }
};
</script>

<style scoped>

</style>
